package com.lntu.meeting.controller;

import com.alibaba.fastjson.JSON;

import com.lntu.meeting.ReserveForm.ReserveForm;
import com.lntu.meeting.service.impl.ReserveServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

import static com.lntu.meeting.common.checkDate.judgmentTime;
import static com.lntu.meeting.common.regexData.regexAllNumber;

/**
 *  获取前端参数 进行判断 传到数据库
 */
@RestController
public class ReserveController {

 //注入Service层
 @Autowired(required=false)
 private ReserveServiceImp reserveServiceImp;

 /**
 *  
 * @param :  reserveForm 前端传回的表单格式
 * @return : true        则为可以插入
 * @return : false       则为不可以插入
  *
 */
 //路径记得自己修改一下哈
 @PostMapping("/qwe")
 public String ReserveController( @RequestBody ReserveForm reserveForm){
  System.out.println(reserveForm);
  //返回前端状态值
  String flag = "false";
  String res = "";
  Boolean code = false ;
if (!reserveForm.getLx()){
  reserveForm.setEndDate(reserveForm.getDate());
}
  //判断格式是否符合，因为可能会存在String的整数，所以才要转型
  //判断开始时间是否大约结束时间，同时预约结束时间是否大约21点
    if (judgmentTime(reserveForm)&&regexAllNumber(reserveForm)){

     reserveForm.setRoomNo(Integer.valueOf(reserveForm.getRoomNo()));
     reserveForm.setStartTime(Integer.valueOf(reserveForm.getStartTime()));
     reserveForm.setEndTime(Integer.valueOf(reserveForm.getEndTime()));
     reserveForm.setUserId(Integer.valueOf(reserveForm.getUserId()));

     //调用Service层进行判断
     code =  reserveServiceImp.selectDate(reserveForm);

   } else {
   Map<String,Object> resMap = new HashMap<>();
   resMap.put("falg", flag);
   res = JSON.toJSONString(resMap);
  }

  if (code){
   flag = "true";
  }
  Map<String,Object> resMap = new HashMap<>();
  resMap.put("falg", flag);
  res = JSON.toJSONString(resMap);


  return res;
  }

}
